package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.ProjectProgressListAdapter;
import com.isunland.managesystem.adapter.ProjectProgressListAdapter.CallBack;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.ProjectCollectOriginal.ProjectCollectContent;
import com.isunland.managesystem.entity.ProjectProgressOriginal;
import com.isunland.managesystem.entity.ProjectProgressOriginal.ProjectProgressSuper;
import com.isunland.managesystem.utils.LogUtil;
import java.util.ArrayList;
import java.util.HashMap;

public class ProjectProgressListFragment extends BaseListFragment implements CallBack {
  protected static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.ProjectProgressListFragment.EXTRA_CONTENT";
  private final int DATAIL_PROJECT = 1;

  private ProjectCollectContent content;
  private ProjectProgressListAdapter mAdapter;
  private ArrayList<ProjectProgressSuper> mList;
  private ArrayList<String> pstageList = new ArrayList<>();
  private LinearLayout llcontainer;
  private String stageCode = "";
  private String pstageCode = "";

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.projecProgressList);
    content = (ProjectCollectContent) getArguments().getSerializable(EXTRA_CONTENT);
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    //noinspection RestrictedApi
    View header = getLayoutInflater(savedInstanceState).inflate(R.layout.header_back, null, false);
    TextView tvBack = (TextView) header.findViewById(R.id.tv_back);
    llcontainer = (LinearLayout) header.findViewById(R.id.ll_container);
    llcontainer.setVisibility(View.GONE);
    tvBack.setOnClickListener(new OnClickListener() {

      @Override public void onClick(View v) {

        pstageCode = pstageList.get(pstageList.size() - 1);
        if (pstageCode == null || TextUtils.isEmpty(pstageCode)) {
          pstageCode = "";
        }
        stageCode = pstageCode;
        volleyPost();
        pstageList.remove(pstageList.size() - 1);//去掉最后一个节点
        if (pstageList.size() == 0) {
          llcontainer.setVisibility(View.GONE);
        }
      }
    });

    mListview.addHeaderView(header);
  }

  @Override public String getUrl() {

    return ApiConst.ULR_PROJECT_PROGRESS;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("projectId", content.getId());
    params.put("dataStatus", "publish");
    params.put("id", "0");
    params.put("stageCode", stageCode);
    return params;
  }

  @Override public void resolveResponse(String json) {
    ProjectProgressOriginal original = new Gson().fromJson(json, ProjectProgressOriginal.class);
    ArrayList<ProjectProgressSuper> rows = original.getRows();
    if (mList == null) {
      mList = new ArrayList<>();
    }
    mList.clear();
    mList.addAll(rows);
    setAdapter(mList);
  }

  private void setAdapter(ArrayList<ProjectProgressSuper> mList2) {
    if (mAdapter == null) {
      mAdapter = new ProjectProgressListAdapter(getActivity(), mList2, this);
    }
    setListAdapter(mAdapter);
    ((ProjectProgressListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    if (position < 2) return;
    ProjectProgressSuper superContent = mList.get(position - 2);
    Intent i = new Intent(getActivity(), DetailProjectProgressActivity.class);
    i.putExtra(DetailProjectProgressFragment.EXTRA_CONTENT, superContent);
    i.putExtra(DetailProjectProgressFragment.EXTRA_PROJECT_ID, content.getId());//项目id
    i.putExtra(DetailProjectProgressFragment.EXTRA_PROJECT_NAME, content.getProjectName());//项目名
    i.putExtra(DetailProjectProgressFragment.EXTRA_PM_ID, content.getPmId());//项目经理
    startActivityForResult(i, DATAIL_PROJECT);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    int SEARCH_PROJECT = 0;
    if (requestCode == SEARCH_PROJECT) {
      content = (ProjectCollectContent) data.getSerializableExtra(EXTRA_CONTENT);
      volleyPost();
    }
    if (requestCode == DATAIL_PROJECT) {
      String type = data.getStringExtra(DetailProjectProgressFragment.EXTRA_TYPE);
      LogUtil.i("type===" + type);
      //若果是项目验收就刷新项目列表，其他刷新阶段列表
      if ("checked".equals(type)) {
        getActivity().setResult(Activity.RESULT_OK);
        getActivity().finish();
      } else {
        volleyPost();
      }
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static ProjectProgressListFragment newInstence(ProjectCollectContent content) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_CONTENT, content);
    ProjectProgressListFragment mfragment = new ProjectProgressListFragment();
    mfragment.setArguments(bundle);
    return mfragment;
  }

  @Override public void onRefresh(String newstageCode, String newpstageCode) {
    stageCode = newstageCode;
    pstageCode = newpstageCode;
    volleyPost();
    llcontainer.setVisibility(View.VISIBLE);
    pstageList.add(pstageCode);
  }
}
